from datetime import timedelta
from utils.operators.cluster_for_spark_sql_operator import SparkSqlOperator
from jms.dwd.dwd_tmsnew_shipment_dt import jms_dwd__dwd_tmsnew_shipment_dt
from jms.dwd.tab.dwd_tab_barscan_other_base_dt import jms_dwd__dwd_tab_barscan_other_base_dt
from jms.dwd.tab.dwd_tab_barscan_loading_base_dt import jms_dwd__dwd_tab_barscan_loading_base_dt
from jms.dwd.tab.dwd_tab_barscan_unloading_base_dt import jms_dwd__dwd_tab_barscan_unloading_base_dt
from jms.dwd.tab.dwd_tab_barscan_centersend_base_dt import jms_dwd__dwd_tab_barscan_centersend_base_dt
from jms.dwd.tab.dwd_tab_barscan_centerarrival_base_dt import jms_dwd__dwd_tab_barscan_centerarrival_base_dt
from jms.dwd.tab.dwd_tab_barscan_sitesend_base_dt import jms_dwd__dwd_tab_barscan_sitesend_base_dt
from jms.dwd.tab.dwd_tab_barscan_sitearrival_base_dt import jms_dwd__dwd_tab_barscan_sitearrival_base_dt
from jms.dwd.oms.dwd_yl_oms_oms_waybill_incre_dt import jms_dwd__dwd_yl_oms_oms_waybill_incre_dt
from jms.dim.dim_tmsnew_vehicle_line import jms_dim__dim_tmsnew_vehicle_line
from jms.dim.dim_tmsnew_vehicle import jms_dim__dim_tmsnew_vehicle
from jms.ods.tms.yl_tmsnew_branch_shipment import jms_ods__yl_tmsnew_branch_shipment
from jms.ods.tms.yl_tmsnew_tms_shipment_stop import jms_ods__yl_tmsnew_tms_shipment_stop
from jms.ods.tms.yl_tmsnew_branch_shipment_stop import jms_ods__yl_tmsnew_branch_shipment_stop

jms_dwd__dwd_wide_tms_road_waybill_dt_v2 = SparkSqlOperator(
    task_id='jms_dwd__dwd_wide_tms_road_waybill_dt_v2',
    task_concurrency=1,
    pool_slots=10,
    master='yarn',
    email=['lukunming@jtexpress.com','yl_bigdata@yl-scm.com'],
    name='jms_dwd__dwd_wide_tms_road_waybill_dt_v2_{{ execution_date | date_add(1) | cst_ds }}',
    sql='jms/dwd/dwd_wide_tms_road_waybill_dt_v2/execute.hql',
    driver_cores=4,
    driver_memory='8g',
    executor_cores=8,
    executor_memory='24G',
    num_executors=90,  # spark.dynamicAllocation.enabled 为 True 时，num_executors 表示最少 Executor 数
    conf={'spark.dynamicAllocation.enabled': 'true',  # 动态资源开启
          'spark.shuffle.service.enabled': 'true',  # 动态资源 Shuffle 服务开启
          'spark.dynamicAllocation.maxExecutors': 100,  # 动态资源最大扩容 Executor 数
          'spark.dynamicAllocation.cachedExecutorIdleTimeout': 60,  # 动态资源自动释放闲置 Executor 的超时时间(s)
          'spark.sql.sources.partitionOverwriteMode': 'dynamic',  # 允许删改已存在的分区
          'spark.executor.memoryOverhead': '3G',  # 堆外内存
          'spark.sql.shuffle.partitions': 2400,

          'spark.memory.useLegacyMode': 'true',
          'spark.shuffle.memoryFraction': '0.8',
          'spark.shuffle.io.retryWait': '60',
          'spark.reducer.maxSizeInFlight': '96M',
          'spark.shuffle.file.buffer': '96k',
          'spark.shuffle.spill.compress': 'true',
          'spark.io.compression.codec': 'lz4',
          'spark.io.compression.lz4.blockSize': '192k',
          'spark.driver.extraJavaOptions': '-XX:+UseG1GC',
          'spark.executor.extraJavaOptions':'-XX:+UseG1GC',
          },
    hiveconf={'hive.exec.dynamic.partition': 'true',  # 动态分区
              'hive.exec.dynamic.partition.mode': 'nonstrict',
              'hive.exec.max.dynamic.partitions': 200,  # 每天生成 20 个分区
              'hive.exec.max.dynamic.partitions.pernode': 200,  # 每天生成 20 个分区
              },
    yarn_queue='pro',
    execution_timeout=timedelta(minutes=60),
)

jms_dwd__dwd_wide_tms_road_waybill_dt_v2 << [
    jms_dwd__dwd_tmsnew_shipment_dt,
    jms_dwd__dwd_tab_barscan_other_base_dt,
    jms_dwd__dwd_tab_barscan_loading_base_dt,
    jms_dwd__dwd_tab_barscan_unloading_base_dt,
    jms_dwd__dwd_tab_barscan_centersend_base_dt,
    jms_dwd__dwd_tab_barscan_centerarrival_base_dt,
    jms_dwd__dwd_tab_barscan_sitesend_base_dt,
    jms_dwd__dwd_tab_barscan_sitearrival_base_dt,
    jms_dwd__dwd_yl_oms_oms_waybill_incre_dt,
    jms_dim__dim_tmsnew_vehicle_line,
    jms_dim__dim_tmsnew_vehicle,
    jms_ods__yl_tmsnew_branch_shipment,
    jms_ods__yl_tmsnew_tms_shipment_stop,
    jms_ods__yl_tmsnew_branch_shipment_stop
]
